<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <ul>
        <li>111</li>
        <li>222</li>
        <li>333</li>
    </ul>
    <script>
        //对象是非线性的，所以没有封装遍历器接口
        //但是可以借助Array类原型的遍历器接口！
        let obj = {
            0: "twj",
            1: "wdf",
            2: "jjb",
            length: 3,
            [Symbol.iterator]: Array.prototype[Symbol.iterator]
        }
        for (let i of obj) {
            console.log(i)
        }

        //手动埋一个遍历器接口
        let obj2 = {
            code: 200,
            name: "obj2",
            list: ["jjb", "twj", "wdf"],
            [Symbol.iterator]() {
                let index = 0
                return {
                    next: () => {
                        return {
                            value: this.list[index++],
                            done: index === (this.list.length + 1) ? true : false
                        }
                    }
                }
            }
        }
        //for..of成功！
        for (let i of obj2) {
            console.log(i)
        }
        //没毛病！
        let iter = obj2[Symbol.iterator]()
        console.log(iter)
        console.log(iter.next())
        console.log(iter.next())
        console.log(iter.next())
        console.log(iter.next())

        //展开运算符会默认调用对象中的遍历器，配合解构赋值就能自动转换成数组！
        console.log([...obj2]) //['jjb', 'twj', 'wdf']

        let oli = document.querySelectorAll("li");
        console.log([...oli]) //[li, li, li]
        console.log(Array.from(oli)) //[li, li, li]
    </script>
</body>

</html>